﻿#include "qtmbackendtask.h"
#include <QApplication>
#include <QJsonDocument>
#include <QSettings>
#include <QDir>

QTmBackendTask::QTmBackendTask()
{
    pr_bHaveExScreen = false;
    pr_pObjRoot = NULL;
    pr_pDesk = QApplication::desktop();
    connect(pr_pDesk, SIGNAL(screenCountChanged(int)),this, SLOT(slot_screenCountChanged(int)), Qt::DirectConnection);
//    pr_tmRepeatWork = new QTimer();
//    connect(pr_tmRepeatWork,SIGNAL(timeout()), this, SLOT(slot_tm_repeat()), Qt::DirectConnection);
    pr_pThread = new QTmThreadRepeat(this);
}

bool QTmBackendTask::init(QObject *rootObj)
{
    pr_pObjRoot = rootObj;
    if( !rootObj ){
        return false;
    }
//    qDebug()<<"Screen num:"<<pr_pDesk->screenCount();
    slot_screenCountChanged(pr_pDesk->screenCount());

    pr_pThread->start();

    return true;
}

void QTmBackendTask::proc_repeat_dir(int nTime ,QString strPath)
{
//    if(pr_tmRepeatWork->isActive()){
//        pr_tmRepeatWork->stop();
//    }
    pr_pThread->setRepeatTime(-1);
    QDir rootDir(strPath);
    if( !rootDir.exists() ){
        return;
    }
    rootDir.setFilter(QDir::Files|QDir::NoDotAndDotDot);
    QFileInfoList list = rootDir.entryInfoList();
    pr_picStrList.clear();
    pr_picIndex = -1;
    for(int i=0;i<list.size();i++){
        QFileInfo oneFile = list.at(i);
        QString suffix = oneFile.suffix().toLower();
        if( suffix == "png" || suffix == "jpg" || suffix == "bmp" ){
            pr_picStrList.append(oneFile.absoluteFilePath());
        }
    }
    if( pr_picStrList.size()>0 ){
        pr_picIndex = 0;
        qml_repeat_show(pr_picStrList.at(pr_picIndex));
        pr_pThread->setRepeatTime(nTime);
//        pr_tmRepeatWork->start(nTime*1000);
    }
}

void QTmBackendTask::qml_repeat_show(QString strPath)
{
    if( !pr_pObjRoot || !pr_bHaveExScreen){
        return;
    }
    QVariant arg1 = QVariant(strPath);
    QMetaObject::invokeMethod(pr_pObjRoot, "showMedia_repeat",Q_ARG(QVariant, arg1));
}

void QTmBackendTask::qml_ui_show_pic(int nDelay, QString strPath)
{
    if( !pr_pObjRoot || !pr_bHaveExScreen){
        return;
    }
    QVariant arg1 = QVariant(nDelay);
    QVariant arg2 = QVariant(strPath);
    QMetaObject::invokeMethod(pr_pObjRoot, "showMedia_pic",Q_ARG(QVariant, arg1),Q_ARG(QVariant, arg2));
}

void QTmBackendTask::qml_ui_show_video(int nDelay, QString strPath,int nVolume)
{
    if( !pr_pObjRoot || !pr_bHaveExScreen){
        return;
    }
    QVariant arg1 = QVariant(nDelay);
    QVariant arg2 = QVariant(strPath);
    QVariant arg3 = QVariant(nVolume);
    QMetaObject::invokeMethod(pr_pObjRoot, "showMedia_video",Q_ARG(QVariant, arg1),
                              Q_ARG(QVariant, arg2),Q_ARG(QVariant, arg3));
}

void QTmBackendTask::qml_ui_show_barrage(QString strTxt,int nKeep)
{
    if( !pr_pObjRoot || !pr_bHaveExScreen){
        return;
    }
    qDebug()<<"qml_ui_show_barrage----------"<<strTxt;
    QVariant arg1 = QVariant(strTxt);
    QVariant arg2 = QVariant(nKeep);
    QMetaObject::invokeMethod(pr_pObjRoot, "showMedia_barrage",Q_ARG(QVariant, arg1),Q_ARG(QVariant, arg2));
}

void QTmBackendTask::qml_ui_show_clean()
{
    if( !pr_pObjRoot || !pr_bHaveExScreen){
        return;
    }
    QMetaObject::invokeMethod(pr_pObjRoot, "showMedia_clean");
}

void QTmBackendTask::qml_ui_show_vip(QString strPath, QString strName)
{
    if( !pr_pObjRoot || !pr_bHaveExScreen){
        return;
    }
    QVariant arg1 = QVariant(strPath);
    QVariant arg2 = QVariant(strName);
    QMetaObject::invokeMethod(pr_pObjRoot, "showMedia_vip",Q_ARG(QVariant, arg1),Q_ARG(QVariant, arg2));
}
void QTmBackendTask::qml_ui_video_vip(int nType, QString strName)
{
    if(!pr_pObjRoot || !pr_bHaveExScreen){
        return;
    }
    qDebug()<<"qml_ui_video_vip----------"<<nType<<strName;
    QVariant arg1 = QVariant(nType);
    QVariant arg2 = QVariant(strName);
    QMetaObject::invokeMethod(pr_pObjRoot,"showVideo_vip",Q_ARG(QVariant,arg1),Q_ARG(QVariant,arg2));

}
void QTmBackendTask::slot_tm_repeat()
{
    if( pr_picIndex<0 ){
        return;
    }
    pr_picIndex += 1;
    if( pr_picIndex>=pr_picStrList.size() ){
        pr_picIndex = 0;
    }
    qDebug()<<"pr_picIndex:"<<pr_picIndex;
    qml_repeat_show(pr_picStrList.at(pr_picIndex));

    //emit sig_com_send("repeat", 0, pr_picStrList.at(pr_picIndex));
}

void QTmBackendTask::slot_screenCountChanged(int newCount)
{
    for(int i=0; i<newCount; i++){
        QRect rcShow = pr_pDesk->screenGeometry(i);
        QString strShow = QString("序号 %1, Rect[%2,%3,%4,%5]").arg(i)
                .arg(rcShow.left()).arg(rcShow.top()).arg(rcShow.width()).arg(rcShow.height());
        qDebug()<<strShow.toStdString().data();
    }
    if( !pr_pObjRoot ){
        qDebug()<<"QML Application root object is null!";
        return;
    }
//    pr_bHaveExScreen = true;
//    pr_pObjRoot->setProperty("visible", true);
//    return;

    // work
    if( newCount > 1 ){
        pr_bHaveExScreen = true;
        QRect rcShow = pr_pDesk->screenGeometry(1); // 在第二块屏幕上显示
        pr_pObjRoot->setProperty("x", rcShow.left());
        pr_pObjRoot->setProperty("y", rcShow.top());
        pr_pObjRoot->setProperty("width", rcShow.width());
        pr_pObjRoot->setProperty("height", rcShow.height());
        pr_pObjRoot->setProperty("visible", true);
    }else{
        pr_bHaveExScreen = false;
        pr_pObjRoot->setProperty("visible", false);
    }
}


QTmThreadRepeat::QTmThreadRepeat(QTmBackendTask *pTask)
{
    m_nDelayTime = -1;
    m_pBackTask = pTask;
}

void QTmThreadRepeat::setRepeatTime(int nDelay)
{
    m_nDelayTime = nDelay;
}

void QTmThreadRepeat::run()
{
    long nDelayCount = 0;
    while( true ){
        QCoreApplication::processEvents();
        msleep(10);
        if(!m_pBackTask){
            continue;
        }
        if( m_nDelayTime < 0 ){
            nDelayCount = 0;
            continue;
        }
        nDelayCount += 1;
        if( m_nDelayTime == 0 ){
            m_nDelayTime = 86400; // 0 当作一天不换图片
        }
        if( nDelayCount > 95*m_nDelayTime ){
            nDelayCount = 0;
            m_pBackTask->slot_tm_repeat();
        }
    }
}
